package 蓝桥杯_2013;

public class Test04_三部排序 {

    static void sort(int[] x) {
        int p = 0;
        int left = 0;
        int right = x.length - 1;

        while (p <= right) {
            // 元素小于0，交换元素与left的位置
            if (x[p] < 0) {
                int t = x[left];
                x[left] = x[p];
                x[p] = t;
                left++;
                p++;
                // 元素大于0，将元素与right的位置交换
            } else if (x[p] > 0) {
                int t = x[right];
                x[right] = x[p];
                x[p] = t;
                right--;
                // 元素等于0
            } else {
                p++;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {25,18,-2,0,16,-5,19,-16,25,-3,0};
        sort(arr);
        for (int i = 0; i < arr.length ; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
